//
//  ClubForumAdminSettingViewController.swift
//  77Club
//
//  Created by Apple on 2024/12/31.
//

import UIKit

class ClubForumAdminSettingViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("管理论坛").color(.black).font(16.font_semiBold).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubSquareSettingItemCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 64.0.Scale
        return tableView
    }()
    
    var banNum = 0
    var memberNum = 0
    var permModel = ClubForumAdminPermModel()
    var master_user: ClubForumMemberModel?
    var dataSource = [ClubSquareSettingModel]()
    var communityId = ""{
        didSet{
            loadData()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.leading(56)
        titleLabel.CenterY == backButton.CenterY
        tableView.fillContainer().top(.navi_height).bottom(.safe_bottom)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }

}

extension ClubForumAdminSettingViewController: UITableViewDelegate, UITableViewDataSource  {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubSquareSettingItemCell", for: indexPath) as! ClubSquareSettingItemCell
        cell.model = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let titleStr = dataSource[indexPath.row].title
        if titleStr == "论坛公告" {
            //论坛公告
            let noticeVC = ClubEditSquareNoticeViewController()
            noticeVC.noticeStr = dataSource[indexPath.row].content
            noticeVC.communityId = communityId
            noticeVC.updateSquareNotice = { notice in
                self.dataSource[2].content = notice
                self.tableView.reloadData()
            }
            self.navigationController?.pushViewController(noticeVC, animated: true)
        }else if titleStr == "论坛成员" || titleStr == "人员禁言" {
            
            let memberVC = ClubSquareMemberViewController()
            memberVC.isAdmin = true
            memberVC.isMute = self.permModel.perm_ban == 1
            if self.permModel.perm_ban == 1 {
                memberVC.isShowBan = true
            }else{
                memberVC.isShowBan = false
            }
            memberVC.isShowManager = false
            memberVC.communityId = communityId
            memberVC.master_user = master_user
            memberVC.memberNum = self.memberNum
            memberVC.banNum = self.banNum
            if titleStr == "论坛成员" {
                memberVC.selIndex = 0
            }else{
                memberVC.selIndex = 1
            }
            memberVC.updateSquareMember = {
                self.loadData()
            }
            self.navigationController?.pushViewController(memberVC, animated: true)
        }else if titleStr == "关注设置" {
            //关注设置
            let settingVC = ClubFollowSettingViewController()
            settingVC.communityId = communityId
            settingVC.model = dataSource[indexPath.row]
            settingVC.updateSquareSetting = { model in
                self.dataSource[indexPath.row] = model
                self.tableView.reloadData()
            }
            self.navigationController?.pushViewController(settingVC, animated: true)
        }
    }
}

//MARK: data
extension ClubForumAdminSettingViewController {
    
    private func loadData(){
        
        ToastUtil.show()
        self.dataSource.removeAll()
        Task {
            let res = await self.network.request(.communityTop(communityID: self.communityId),type: ClubGameCommunityModel.self)
            ToastUtil.dismiss()
            if res.isSuccess, let model = res.model {
                self.master_user = model.master_user
                if self.permModel.perm_notice == 1 {
                    var noticeContent = model.notice
                    if noticeContent.count <= 0 {
                        noticeContent = "未设置"
                    }
                    let noticeMd = ClubSquareSettingModel(title: "论坛公告", content: noticeContent)
                    self.dataSource.append(noticeMd)
                }
                if self.permModel.perm_ban == 1 {
                    var banNumber = ClubGlobalTool.shared.formatNumber(num: "\(model.ban_num)")
                    self.banNum = model.ban_num
                    if banNumber == "0" {
                        banNumber = ""
                    }else{
                        banNumber = banNumber + "人"
                    }
                    let banNumberMd = ClubSquareSettingModel(title: "人员禁言", content: banNumber)
                    self.dataSource.append(banNumberMd)
                }
                var guanzhuNum = ClubGlobalTool.shared.formatNumber(num: "\(model.guanzhu_num)")
                self.memberNum = model.guanzhu_num
                if guanzhuNum == "0" {
                    guanzhuNum = ""
                }else{
                    guanzhuNum = guanzhuNum + "人"
                }
                let guanzhuNumMd = ClubSquareSettingModel(title: "论坛成员", content: guanzhuNum)
                self.dataSource.append(guanzhuNumMd)
                if self.permModel.perm_guanzhu == 1 {
                    var setting = model.settings
                    if setting.count <= 0 {
                        setting = "未设置"
                    }
                    let settingMd = ClubSquareSettingModel(title: "关注设置", content: setting, logo: "\(model.guanzhu_num)", bgImg: model.system_type)
                    self.dataSource.append(settingMd)
                }
                self.tableView.reloadData()
            }
        }
    }
}
